package com.assets.asset.annotation.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.assets.asset.mapper.TReceiveMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 这个是领用表的
 * 用aop将存使用公司核使用部门的id号转换为名称
 * 以及将资产表中的是否领用状态转变
 * 还有将领用人更改
 */
@Aspect
@Component
public class ReceiveComDeptIdAOP {


    @Autowired
    private TReceiveMapper receiveMapper;

    @Around("@annotation(com.assets.asset.annotation.ReceiveComDeptID)")
//    public void around(JoinPoint point) throws Throwable {
    public Object around(ProceedingJoinPoint point) throws Throwable {


        //获取当前
//        Object[] args = joinPoint.getArgs();
        Object[] args = point.getArgs();

        Object result = point.proceed(args);

        String toJSONString = JSON.toJSONString(args);
        System.out.println("toJSONString = " + toJSONString);


        String s = toJSONString.replace("]", "");
        String replace = s.replace("[", "");
        System.out.println(replace);

        JSONObject jsonObject = JSON.parseObject(replace);
        String assetCode = jsonObject.getString("assetCode");
        //获取领用人
        String assetUser = jsonObject.getString("assetUser");


        //转名称
        receiveMapper.updateComAndDept(assetCode);

        //赋予领用状态以及将资产状态改为在用
        receiveMapper.receiveAsset(assetCode);

        //更改领用人
        receiveMapper.updateAssetRecipient(assetCode,assetUser);

        return result;


    }


}

